<?php

class JokesController extends Controller {

	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout = '//layouts/column1';

	/**
	 * Displays the jokes from a category.
	 * @param integer $id the ID of the category
	 */
	public function actionIndex() {
		$categories = JokeCategory::model()->findAll();

		$this->render('index', array(
			'categories' => $categories,
		));
	}

	public function actionView($id) {
		$categoryId = $id;
		$currentCategory = JokeCategory::model()->findByPk($categoryId);
		if ($currentCategory === null) {
			throw new CHttpException(404, 'The requested joke category does not exist.');
		}
		$categories = JokeCategory::model()->findAll();

		$jokesProvider = new CActiveDataProvider('JokeContent', array(
			'criteria' => array(
				'condition' => 'valid = 1 and category_id = :category_id',
				'order' => 'id',
				'params' => array(':category_id' => $categoryId)
			),
			'pagination' => array(
				'pageSize' => 15,
			),
		));

		$this->render('view', array(
			'categories' => $categories,
			'jokesProvider' => $jokesProvider,
			'currentCategory' => $currentCategory,
		));
	}

	/**
	 * Add a new joke for a category.
	 * @param integer $id the ID of the category
	 */
	public function actionAdd($category_id) {
		$model = new JokeContent;
		$model->category_id = $category_id;

		$categories = JokeCategory::model()->findAll();

		if (isset($_POST['JokeContent'])) {
			$model->attributes = $_POST['JokeContent'];
			$model->created_on = date("Y-m-d H:i:s");
			$model->last_modified_on = date("Y-m-d H:i:s");
			$model->user_id = Yii::app()->user->id;
			$model->grades_total = 0;
			$model->grades_number = 0;
			$model->valid = 1;
			if ($model->save())
				$this->redirect(array('view', 'id' => $model->category_id));
		}

		$this->render('add', array(
			'model' => $model,
			'categories' => $categories,
		));
	}

	/**
	 * Updates a joke.
	 * @param integer $id the ID of the joke
	 */
	public function actionUpdate($id) {
		$model=$this->loadModel($id);

		if( $model->user_id != Yii::app()->user->id ){
			throw new CHttpException(404,'The joke is not yours.');
		}

		$categories = JokeCategory::model()->findAll();

		if (isset($_POST['JokeContent'])) {
			$existingModel=$model;
			$model->attributes = $_POST['JokeContent'];

			$existingModel->title = $model->title;
			$existingModel->text = $model->text;
			$existingModel->category_id = $model->category_id;
			$existingModel->last_modified_on = date("Y-m-d H:i:s");

			if ($model->save())
				$this->redirect(array('view', 'id' => $model->category_id));
		}

		$this->render('add', array(
			'model' => $model,
			'categories' => $categories,
		));
	}

	/**
	 * Delete a joke.
	 * @param integer $id the ID of the joke
	 */
	public function actionDelete($id) {
		$model=$this->loadModel($id);

		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));


		$this->render('add', array(
			'model' => $model,
			'categories' => $categories,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return JokeContent the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=JokeContent::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	// Uncomment the following methods and override them if needed
	/*
	  public function filters()
	  {
	  // return the filter configuration for this controller, e.g.:
	  return array(
	  'inlineFilterName',
	  array(
	  'class'=>'path.to.FilterClass',
	  'propertyName'=>'propertyValue',
	  ),
	  );
	  }

	  public function actions()
	  {
	  // return external action classes, e.g.:
	  return array(
	  'action1'=>'path.to.ActionClass',
	  'action2'=>array(
	  'class'=>'path.to.AnotherActionClass',
	  'propertyName'=>'propertyValue',
	  ),
	  );
	  }
	 */
}